/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package me.samlss.lighter.animator;

import me.samlss.lighter.interceptor.BaseInterpolator;
import me.samlss.lighter.util.LogUtil;
import ohos.agp.animation.AnimatorValue;
import ohos.agp.components.Component;

import java.util.List;

/**
 * 自定义动画相关差值器和属性
 *
 * @author: zhu_zhonglin
 * @time: 2021/4/25
 */
public class CustomAnimatorValue extends AnimatorValue {
    /**
     * 属性动画集合类
     */
    private AnimatorAttr animatorAttr;
    /**
     * 差值器
     */
    private BaseInterpolator interpolator;

    public CustomAnimatorValue(AnimatorAttr animatorAttr) {
        this.animatorAttr = animatorAttr;
    }

    public CustomAnimatorValue(AnimatorAttr animatorAttr, BaseInterpolator interpolator) {
        this.animatorAttr = animatorAttr;
        this.interpolator = interpolator;
    }

    /**
     * 启动动画
     * @param target 指定动画展示的组件位置
     */
    public void animatorStart(Component target) {
        if (interpolator != null && animatorAttr != null) {
            List<String> propertyValues = animatorAttr.propertyValues;
            float fromXDelta = animatorAttr.fromXDelta;
            float toXDelta = animatorAttr.toXDelta;
            float fromYDelta = animatorAttr.fromYDelta;
            float toYDelta = animatorAttr.toYDelta;
            this.setValueUpdateListener((animatorValue, percent) -> {
                float interpolation = interpolator.getInterpolation(percent);
                float targetX = fromXDelta + (toXDelta - fromXDelta) * interpolation;
                float targetY = fromYDelta + (toYDelta - fromYDelta) * interpolation;
                if (propertyValues.contains("scaleX")) {
                    LogUtil.loge("===scaleX:"+ targetX);
                    target.setScaleX(targetX);
                }
                if (propertyValues.contains("scaleY")) {
                    LogUtil.loge("===scaleY" + targetY);
                    target.setScaleY(targetY);
                }
                if (propertyValues.contains("translateX")) {
                    target.setTranslationX(targetX);
                }
                if (propertyValues.contains("translateY")) {
                    target.setTranslationY(targetY);
                }
            });
        }
        this.start();
    }

    /**
     * 停止动画
     */
    public void animatorStop() {
        if (mUpdateListener != null) {
            this.setValueUpdateListener(null);
        }
        this.stop();
    }

    /**
     * 获取差值器
     * @return 差值器基类
     */
    public BaseInterpolator getInterpolator() {
        return interpolator;
    }

    /**
     * 设置差值器
     * @param interpolator 差值器基类
     */
    public void setInterpolator(BaseInterpolator interpolator) {
        this.interpolator = interpolator;
    }
}
